<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="../../layui/css/layui.css" media="all">
<link rel="stylesheet" href="../../layui/css/formSelects-v4.css" media="all">
<body>
<hr>
<!--表格上方工具栏-->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <shiro:hasPermission name="permition:add">
            <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="addPermition()" lay-event=""><i
                    class="layui-icon">&#xe640;</i>添加
            </button>
        </shiro:hasPermission>
    </div>
</script>

<!--表格工具列-->
<script type="text/html" id="barDemo">
    <shiro:hasPermission name="permition:modify">
        <a class="layui-btn layui-btn-xs" lay-event="modify" style="background: orange"> <i class="layui-icon">&#xe63c;</i>编辑</a>
    </shiro:hasPermission>

    <shiro:hasPermission name="permition:delete">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"> <i class="layui-icon">&#xe640;</i>删除</a>
    </shiro:hasPermission>

</script>
<table class="layui-hide" id="test" lay-filter="test"></table>
</body>

<script>

    layui.use(["table", "form", "treeGrid"], function () {
        var table = layui.table;
        var treeGrid = layui.treeGrid;

        $.get("/permition/permitionList", function (d) {
            $.each(d.data, function () {
                this.isOpen = true;
            });
            treeGrid.render({
                id: 'perTable',
                elem: '#test',
                data: d.data,
                idField: 'id',
                treeId: 'id',
                treeUpId: 'parentId',
                treeShowName: 'permitionName',
                toolbar: '#toolbarDemo',
                cols: [[
                    {field: 'permitionName', title: '权限名称'},
                    {
                        field: 'permitionType', title: '权限类型',
                        templet: function (d) {
                            if(d.permitionType ==1){
                                return "<button class='layui-btn layui-btn-xs'>菜单</button>"
                            }else if(d.permitionType==2){
                                return "<button class='layui-btn layui-btn-danger layui-btn-xs'>按钮</button>"
                            }
                        }
                    },
                    {field: 'permitionDesc', title: '权限描述'},
                    {field: 'permitionUrl', title: '连接地址'},
                    {field: 'permitionCode', title: '授权码'},
                    {title: '工具', toolbar: '#barDemo'}
                ]]
            });
        });

        treeGrid.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url: "/permition/deletePermitionInfo",
                        data: {permitionId: data.id},
                        success: function (result) {
                            if (0 == result.code) {
                                layer.msg(result.msg, {icon: 1, time: 1000});
                                obj.del();
                                layer.close(index);
                                //刷新首页
                            } else {
                                layer.msg(result.msg, {icon: 5, time: 1000});
                            }
                        }
                    });
                });
            }else  if(obj.event === 'modify'){
                var lay = layer.open({
                    type: 2,
                    title: "权限信息修改",
                    shade: 0.4,
                    area: ['800px', '600px'],
                    maxmin: true,
                    shadeClose: true,
                    content: '/doModifyPermitionInfo',
                    fix: false, //不固定
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                        //获取子页面的元素，进行数据渲染
                        body.contents().find("#id").val(data.id);
                        body.contents().find('#permitionName').val(data.permitionName);
                        body.contents().find('#permitionDesc').val(data.permitionDesc);
                        body.contents().find('#permitionUrl').val(data.permitionUrl);
                        body.contents().find('#permitionCode').val(data.permitionCode);
                        var radioObjs = body.contents().find('.radioClass');
                        for (var i = 0; i < radioObjs.length; i++) {
                            var radioObj = radioObjs[i];
                            debugger;
                            if (radioObj.value == data.permitionType) {
                                radioObj.checked = true;
                                break;
                            }
                        }
                    }
                });
            }
        })
    });

    function dateToStr(date) {
        if (null == date || "" === date) {
            return "";
        }
        var time = new Date(date);
        var y = time.getFullYear();
        var M = time.getMonth() + 1;
        M = M < 10 ? ("0" + M) : M;
        var d = time.getDate();
        d = d < 10 ? ("0" + d) : d;
        var h = time.getHours();
        h = h < 10 ? ("0" + h) : h;
        var m = time.getMinutes();
        m = m < 10 ? ("0" + m) : m;
        var s = time.getSeconds();
        s = s < 10 ? ("0" + s) : s;
        var str = y + "-" + M + "-" + d + " " + h + ":" + m + ":" + s;
        return str;
    }

    function searchData() {
        layui.table.reload("test", {
            page: {
                curr: 1
            },
            where: {
                "username": $("#username").val(),
                "roleIds": layui.formSelects.value('roles', 'valStr')
            }
        });
    }

    function addPermition() {
        var lay = layer.open({
            type: 2,
            title: "权限信息添加",
            shade: 0.4,
            area: ['800px', '600px'],
            maxmin: true,
            shadeClose: true,
            content: '/doAddPermitionInfo',
            fix: false, //不固定
            success: function (layero, index) {

            }
        });
    }
</script>
